package com.ankur.calllogger.dao;

import java.util.Date;

import javax.persistence.Id;

import org.json.JSONException;
import org.json.JSONObject;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Unindexed;

@Cached
public class CallDetail {
	@Id
	private String id;
	private String email;
	private long callId;
	// Mobile Number
	private String number;
	// Call in seconds
	private long duration;
	// Number of milliseconds since 1971
	
	private Date date;
	// Name of Caller (Optional)
	@Unindexed
	private String name;
	// 1:Incoming 2:Outgoing 3:Missed Call
	private int type;
	private Key<Account> owner;
	
	public CallDetail() {
		super();
	}
	
	public CallDetail(Key<Account> owner, String deviceId, String email, long callId, String number, long duration, Date date, String name, int type) {
		this(deviceId, email, callId, number, duration, date, name, type);
		this.owner=owner;
	}
	
	public CallDetail(String deviceId, String email, long callId, String number, long duration, Date date, String name, int type) {
		this.id=deviceId+email+callId;
		this.email=email;
		this.callId=callId;
		this.number=number;
		this.duration=duration;
		this.date=date;
		this.name=name;
		this.type=type;
	}
	
	public void setOwner(Account owner) {
		this.owner=new Key<Account>(Account.class, owner.getEmail());
	}
	
	public int getType() {
		return type;
	}
	
	public String getNumber() {
		return number;
	}
	
	public long getDuration() {
		return duration;
	}
	
	public String getName() {
		return name;
	}
	
	public Date getDate() {
		return date;
	}

	@Override
	public String toString() {
		return new StringBuilder("Call Detail :").append("\nNumber : ").append(
				number).append("\nDuration : ").append(duration).append(
				"\nDate : ").append(date).append("\nCall Id : ").append(callId)
				.append("\nName : ").append(name).append("\nCall Type : ")
				.append(type).toString();
	}
	
	public JSONObject toJSON() {
		JSONObject json=new JSONObject();
		try {
			json.put("callId", this.callId);
			json.put("date", this.date.getTime());
			json.put("duration", this.duration);
			json.put("name", this.name);
			json.put("number", this.number);
			json.put("type", this.type);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return json;
	}
}
